package com.wxproject.demo.Utils;

import com.wxproject.demo.Common.lang.ResultBody;
import com.wxproject.demo.Entity.Model.UpLoadFileDO;
import com.wxproject.demo.Service.ImgAndWordVerify.ImgCensor;
import com.wxproject.demo.Service.UserFileService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author zqf
 * @date 2021/4/5 14:03
 * @description
 */

@Component
public class UploadUtil {
    @Autowired
    private UserFileService staticUserFileService;
    @Autowired
    private static UserFileService userFileService;
    @PostConstruct
    public void init() {
        userFileService = staticUserFileService;
    }

    private static String realPath = "/www/wwwroot/files/static/file";
    private static String url = "https://www.wwsszz.top/static/file/";

    //上传人脸视频
    public static String upload(MultipartFile video) throws IOException {
        String newFileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())+ UUID.randomUUID().toString().replace("-","")+"."+ FilenameUtils.getExtension(video.getOriginalFilename());
        String dateDirPath = realPath +"/";
        File dateDir = new File(dateDirPath);
        if(!dateDir.exists()){
            dateDir.mkdirs();
        }
        video.transferTo(new File(dateDir,newFileName));
        return newFileName;
    }

    //上传用户文件
    public static ResultBody uploadFile(MultipartFile file, HttpServletRequest request,String folderId) throws IOException {
        UpLoadFileDO userFile = new UpLoadFileDO();
        String id = JWTUtil.getUserId(request.getHeader("Token"));
        String oldFileName = file.getOriginalFilename();
        String extension = "." + FilenameUtils.getExtension(file.getOriginalFilename());
        String newTime = DateUtils.getDateShort();
        String newFileName = newTime + UUID.randomUUID().toString().replace("-", "") + extension;
        long size = file.getSize();
        String type = file.getContentType();
        String dateFormat = DateUtils.getDateShort();
        String dateDirPath = realPath + "/" + dateFormat;
        File dateDir = new File(dateDirPath);
        if (!dateDir.exists()) {
            dateDir.mkdirs();
        }
        String fileId = String.valueOf(SnowFlake.nextId());
        userFile.setHttpUrl(url + newTime + "/" + newFileName);
        file.transferTo(new File(dateDir, newFileName));
        userFile.setId(fileId);
        userFile.setOldFileName(oldFileName);
        userFile.setNewFileName(newFileName);
        userFile.setUserId(id);
        userFile.setFolderId(folderId);
        userFile.setSize(String.valueOf(size));
        userFile.setType(type);
        userFile.setPath(dateDirPath);
        if (userFileService.uploadFile(userFile) != 0) {
            return ResultBody.ok().message("上传成功").data("id",fileId);
        }
        return ResultBody.error().message("上传失败");
    }

    //上传公开文件
    public static ResultBody uploadPublicFile(MultipartFile file, HttpServletRequest request,String itr) throws IOException {
        UpLoadFileDO userFile = new UpLoadFileDO();
        String id = JWTUtil.getUserId(request.getHeader("Token"));
        String oldFileName = file.getOriginalFilename();
        String extension = "." + FilenameUtils.getExtension(file.getOriginalFilename());
        String newTime = DateUtils.getDateShort();
        String newFileName = newTime + UUID.randomUUID().toString().replace("-", "") + extension;
        long size = file.getSize();
        String type = file.getContentType();
        String dateFormat = DateUtils.getDateShort();
        String dateDirPath = realPath + "/" + dateFormat;
        File dateDir = new File(dateDirPath);
        if (!dateDir.exists()) {
            dateDir.mkdirs();
        }
        String fileId = String.valueOf(SnowFlake.nextId());
        userFile.setHttpUrl(url + newTime + "/" + newFileName);
        file.transferTo(new File(dateDir, newFileName));
        userFile.setId(fileId);
        userFile.setOldFileName(oldFileName);
        userFile.setNewFileName(newFileName);
        userFile.setUserId(id);
        userFile.setIntroduction(itr);
        userFile.setSize(String.valueOf(size));
        userFile.setType(type);
        userFile.setPath(dateDirPath);
        if (userFileService.uploadPublicFile(userFile) != 0) {
            return ResultBody.ok().message("上传成功").data("id",fileId);
        }
        return ResultBody.error().message("上传失败");
    }


    //上传轮播图
    public static ResultBody uploadFile(MultipartFile file) throws IOException {
        UpLoadFileDO userFile = new UpLoadFileDO();
        String oldFileName = file.getOriginalFilename();
        String extension = "." + FilenameUtils.getExtension(file.getOriginalFilename());
        String newTime = DateUtils.getDateShort();
        String newFileName = newTime + UUID.randomUUID().toString().replace("-", "") + extension;
        long size = file.getSize();
        String type = file.getContentType();
        String dateFormat = DateUtils.getDateShort();
        String dateDirPath = realPath + "/" + dateFormat;
        File dateDir = new File(dateDirPath);
        if (!dateDir.exists()) {
            dateDir.mkdirs();
        }
        userFile.setHttpUrl(url + newTime + "/" + newFileName);
        file.transferTo(new File(dateDir, newFileName));
        userFile.setId(String.valueOf(SnowFlake.nextId()));
        userFile.setOldFileName(oldFileName);
        userFile.setNewFileName(newFileName);
        userFile.setSource(0);
        userFile.setSize(String.valueOf(size));
        userFile.setType(type);
        userFile.setPath(dateDirPath);
        if (userFileService.uploadFile(userFile) != 0) {
            return ResultBody.ok().message("上传成功");
        }
        return ResultBody.error().message("上传失败");
    }

    //上传图片返回链接
    public static ResultBody uploadFileReturnUrl(MultipartFile file) throws IOException {
        UpLoadFileDO userFile = new UpLoadFileDO();
        String oldFileName = file.getOriginalFilename();
        String extension = "." + FilenameUtils.getExtension(file.getOriginalFilename());
        String newTime = DateUtils.getDateShort();
        String newFileName = newTime + UUID.randomUUID().toString().replace("-", "") + extension;
        long size = file.getSize();
        String type = file.getContentType();
        String dateFormat = DateUtils.getDateShort();
        String dateDirPath = realPath + "/" + dateFormat;
        File dateDir = new File(dateDirPath);
        if (!dateDir.exists()) {
            dateDir.mkdirs();
        }
        userFile.setHttpUrl(url + newTime + "/" + newFileName);
        file.transferTo(new File(dateDir, newFileName));
        userFile.setId(String.valueOf(SnowFlake.nextId()));
        userFile.setOldFileName(oldFileName);
        userFile.setNewFileName(newFileName);
        userFile.setSize(String.valueOf(size));
        userFile.setType(type);
        userFile.setSource(1);
        userFile.setPath(dateDirPath);
        if (userFileService.uploadFile(userFile) != 0) {
            return ResultBody.ok().data("url",userFile.getHttpUrl());
        }
        return ResultBody.error().message("上传失败");
    }



    //上传人脸图片返回链接
    public static ResultBody uploadFaceFileReturnUrl(MultipartFile file) throws IOException {
        UpLoadFileDO userFile = new UpLoadFileDO();
        String oldFileName = file.getOriginalFilename();
        String extension = "." + FilenameUtils.getExtension(file.getOriginalFilename());
        String newTime = DateUtils.getDateShort();
        String newFileName = newTime + UUID.randomUUID().toString().replace("-", "") + extension;
        long size = file.getSize();
        String type = file.getContentType();
        String dateFormat = DateUtils.getDateShort();
        String dateDirPath = realPath + "/" + dateFormat;
        File dateDir = new File(dateDirPath);
        if (!dateDir.exists()) {
            dateDir.mkdirs();
        }
        userFile.setHttpUrl(url + newTime + "/" + newFileName);
        file.transferTo(new File(dateDir, newFileName));
        userFile.setId(String.valueOf(SnowFlake.nextId()));
        userFile.setOldFileName(oldFileName);
        userFile.setNewFileName(newFileName);
        userFile.setSize(String.valueOf(size));
        userFile.setType(type);
        userFile.setSource(1);
        userFile.setPath(dateDirPath);
        if (userFileService.uploadFile(userFile) != 0) {
            return ResultBody.ok().data("url",userFile.getHttpUrl());
        }
        return ResultBody.error().message("上传失败");
    }



    //上传图片检查图片返回链接
    public static ResultBody uploadFileReturnUrlAndVerifyImg(MultipartFile file) throws IOException {
        UpLoadFileDO userFile = new UpLoadFileDO();
        String oldFileName = file.getOriginalFilename();
        String extension = "." + FilenameUtils.getExtension(file.getOriginalFilename());
        String newTime = DateUtils.getDateShort();
        String newFileName = newTime + UUID.randomUUID().toString().replace("-", "") + extension;
        long size = file.getSize();
        String type = file.getContentType();
        String dateFormat = DateUtils.getDateShort();
        String dateDirPath = "D:\\videos" + "\\" + dateFormat;
        File dateDir = new File(dateDirPath);
        if (!dateDir.exists()) {
            dateDir.mkdirs();
        }
        userFile.setHttpUrl(url + newTime + "/" + newFileName);
        file.transferTo(new File(dateDir, newFileName));
        userFile.setId(String.valueOf(SnowFlake.nextId()));
        userFile.setOldFileName(oldFileName);
        userFile.setNewFileName(newFileName);
        userFile.setSize(String.valueOf(size));
        userFile.setType(type);
        userFile.setSource(1);
        userFile.setPath(dateDirPath);
        String s = ImgCensor.ImgCensor(dateDirPath+"\\"+userFile.getNewFileName());
        if (userFileService.uploadFile(userFile) != 0) {
            return ResultBody.ok().data("url",userFile.getHttpUrl());
        }
        return ResultBody.error().message("上传失败");
    }
}
